/**
 * Created by dengyuanke on 2018/5/8.
 */
new Vue({
    el:"#main-body",
    data:{
        artiscles:[],
        archives:[],
        categories:[],
        current:1,
        pageTotal:0,
        typeFlag:"",
        categoryId:"",
        archiveDate:""
    },
    mounted:function () {
        this.$nextTick(function () {
            this.initView();
        })
    },
    filters:{
        formatDate:function (value) {
            return moment(value).format('YYYY/MM/DD');
        }
    },
    computed: {

    },
    methods: {
        initView:function () {
            let _this=this;
            let type=this.getQueryString("type");
            let category=this.getQueryString("category");
           if("artiscles"==type){
               this.artisclesPage(this.current);
           }
           if("category"==type){
            let id=this.getQueryString("id");
            this.categoryPage(id,this.current);
           }
           if("archive"==type){
               let date=this.getQueryString("date");
               this.archivePage(date,this.current);
           }
        },
        artisclesPage:function(currentPage){
            let _this=this;
            axios.get(url+'/artiscles/artiscles/1'
                , {
                    params: {
                        current: currentPage,
                    }
                }).then(function (response) {
                _this.artiscles=response.data.result;
                _this.pageTotal=response.data.totalSize;
                _this.typeFlag="artiscles";
            })
                .catch(function (error) {
                    console.log(error);
                });
        },
        categoryPage:function (id,currentPage) {
            let _this=this;
            axios.get(url+'artiscles/category/'+id
                , {
                    params: {
                        current: currentPage,
                    }
                }).then(function (response) {
                _this.artiscles=response.data.result;
                _this.pageTotal=response.data.totalSize;
                _this.typeFlag="category";
                _this.categoryId=id;
            })
                .catch(function (error) {
                    console.log(error);
                });
        },
        archivePage:function (date,currentPage) {
            let _this=this;
            axios.get(url+'artiscles/archive/'+date
                , {
                    params: {
                        current: currentPage,
                    }
                }).then(function (response) {
                _this.artiscles=response.data.result;
                _this.pageTotal=response.data.totalSize;
                _this.typeFlag="archive";
                _this.archiveDate=date;
            })
                .catch(function (error) {
                    console.log(error);
                });
        },
        getQueryString:function (name) {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            let r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        },
        changePage:function (currentPage) {
            var _this=this;
            if("artiscles"==this.typeFlag){
                this.artisclesPage(currentPage);
            }
            if("category"==this.typeFlag){
                let id=this.getQueryString("id");
                this.categoryPage(this.categoryId,this.current);
            }
            if("archive"==this.typeFlag){
                this.archivePage(this.archiveDate,this.current);
            }
        },
        archive:function (date) {
            let formatDate=moment(date).format('YYYYMM');
            window.location.href="full-width.html?type=archive&date="+formatDate;
        }
    }
});